/*
 * libc.c 
 */

#include <libc.h>
#include <errno.h>

int errno = 0;

/* Wrapper of  write system call*/
int write(int fd,char *buffer,int size)
{
    int res;
    __asm__ __volatile__(
        "movl 8(%%ebp), %%ebx\n"
        "movl 12(%%ebp), %%ecx\n"
        "movl 16(%%ebp), %%edx\n"
        "movl $4, %%eax\n"
        "int $0x80\n"
        "movl %%eax, %0\n"
        : "=g" (res)
        :
        :"bx"
    );
    if (res < 0) {
        errno = -res;
        res = -1;
    }
    return res;
}
void perror()
{
	switch (errno)
	{
		case 0:
			write(1,"\nNon error ocurred\n",19);
			break;
		case 1:
			write(1,"\n Operation not permitted\n",26); 
			break;
		case 2:
			write(1,"\nNo such file or directory\n",27);
			break;
		case 3:
			write(1,"\nNo such process\n",16);
			break;
		case 4:
			write(1,"\nInterrupted function call\n",26);
			break;
		case 5:
			write(1,"\nInput/output error\n",20); 
			break;
		case 6:
			write(1,"\nNo such device or address\n",26);
			break;
		case 7:
			write(1,"\nArg list too long\n",19);
			break;
		case 8:
			write(1,"\nExec format error\n",19);
			break;
		case 9:
			write(1,"\nBad file descriptor\n",21);
			break;
		case 10:
			write(1,"\nNo child processes\n",20);
			break;
		case 11:
			write(1,"\nResource temporarily unavailable\n",32);
			break;
		case 12:
			write(1,"\nNot enough space\n",17);
			break;
		case 13:
			write(1,"\nPermission denied\n",19);
			break;
		case 14:
			write(1,"\nBad address\n",13);
			break;
		case 16:
			write(1,"\nstrerror reports-Resource device\n",29);
			break;
		case 17:
			write(1,"\nFile exists\n",23);
			break;
		case 18:
			write(1,"\nImproper link (cross-device link?)\n",26);
			break;
		case 19:
			write(1,"\nNo such device\n",26);
			break;
		case 20:
			write(1,"\nNot a directory\n",17);
			break;
		case 21:
			write(1,"\nIs a directory\n",17);
			break;
		case 22:
			write(1,"\nInvalid argument\n",18);
			break;
		case 23:
			write(1,"\nToo many open files in system\n",31);
			break;
		case 24:
			write(1,"\nToo many open files\n",21);
			break;
		case 25:
			write(1,"\nInappropriate I/O control operation\n",37);
			break;
		case 27:
			write(1,"\nFile too large\n",16);
			break;
		case 28:
			write(1,"\nImproper link (cross-device link?)\n",36);
			break;
		case 29:
			write(1,"\nInvalid seek (seek on a pipe?)\n",22);
			break;
		case 30:
			write(1,"\nRead-only file system\n",23);
			break;
		case 31:
			write(1,"\nToo many links\n",16);
			break;
		case 32:
			write(1,"\nBroken pipe\n",13);
			break;
		case 33:
			write(1,"\nDomain error (math functions)\n",31);
			break;
		case 34:
			write(1,"\nResult too large (possibly too small)\n",39);
			break;
		case 36:
			write(1,"\nResource deadlock avoided (non-Cyg)\n",37);
			break;
		case 38:
			write(1,"\nFilename too long (91 in Cyg?)\n",32);
			break;
		case 39:
			write(1,"\nNo locks available (46 in Cyg?)\n",33);
			break;
		case 40:
			write(1,"\nFunction not implemented (88 in Cyg?)\n",39);
			break;
		case 41:
			write(1,"\nDirectory not empty (90 in Cyg?)\n",34);
			break;
		case 42:
			write(1,"\nIllegal byte sequence\n",23);
			break;
		default:
			write(1,"\nNo especified error\n",21);
			break;
	};
}

